<?php

namespace shopend\controllers;

use common\models\Admin;
use common\models\Shop;
use common\service\ConstantService;
use common\service\GlobalUrlService;

class AdminController extends BaseController
{
    /**
     * 这里是登录界面.
     */
    public function actionLogin()
    {

        $this->layout = 'common';
        if($this->isGet())
        {
            return $this->render('login');
        }
        if($this->isPost() && $this->isAjax())
        {
            $data = $this->post();
            if(!array_key_exists('username',$data) || !array_key_exists('pass',$data))
            {
                return $this->renderJson([],'信息不完整',ConstantService::$response_fail);
            }

            // 开始登录.
            $shop_uaer = Shop::findOne(['user_account'=>$data['username']]);

            if(!$shop_uaer)
            {
                return $this->renderJson([],'登录失败,请检查所输入的用户名和密码!',ConstantService::$response_fail);
            }

            // 开始验证正确的密码.
            if($shop_uaer['password'] != md5(md5($data['pass']).$this->salt))
            {
                return $this->renderJson([],'登录失败,请检查所输入的用户名和密码!',ConstantService::$response_fail);
            }
            //验证状态是否正确
            if($shop_uaer['status'] == 0){
                return $this->renderJson([],'登录失败,该商户号还为审核通过,请联系管理员!',ConstantService::$response_fail);

            }
            if($shop_uaer['status'] == 2){
                return $this->renderJson([],'登录失败,该商户审核资料被驳回,请重新修改后提交!',ConstantService::$response_fail);
            }
            if($shop_uaer['status'] == 3){
                return $this->renderJson([],'登录失败,该商户号已停用,请联系管理员!',ConstantService::$response_fail);
            }


            // 这里开始其他操作.记住用户的密码.
            $token = $shop_uaer->primaryKey . '#' . $this->genToken($shop_uaer);
            $this->addCookie($this->auth_cookie, $token,7200);
            $shop_uaer->login_time = time();
            $shop_uaer->save();
            return $this->renderJson([],'登录成功',ConstantService::$response_success);
        }

        return $this->redirect(GlobalUrlService::buildShopUrl('/admin/login'));
    }

    /**
     * 用户退出.
     */
    public function actionLogout()
    {
        $this->removeCookie($this->auth_cookie);
        return $this->redirect(GlobalUrlService::buildShopUrl('/admin/login'));
    }

    /**
     * 修改密码
     */
    public function actionModifyPwd(){
        //前台页面使用弹窗 id 原密码 ,新密码,确认密码
        if($this->isPost() && $this->isAjax()){
            $data = $this->post();

            if(strlen($data['pwd']) < 6){
                return $this->renderJson([],'密码长度至少6位',ConstantService::$response_fail);
            }
            $shop = Shop::findOne(['id'=>$this->shop['id']]);

            if($shop['password'] == md5(md5($data['pwd']).$this->salt)){
                return $this->renderJson([],'原密码和新密码一致',ConstantService::$response_fail);
            }
            if($data['pwd'] != $data['re_pwd']){
                return $this->renderJson([],'两次输入密码不一致',ConstantService::$response_fail);
            }
            $shop['password'] = md5(md5($data['pwd']).$this->salt);
            if($shop->save()){
                return $this->renderJson([],'修改成功',ConstantService::$response_success);
            }
            return $this->renderJson([],'修改失败,请稍后再试',ConstantService::$response_fail);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);
    }
}